﻿using SCG.General;
using SCG.SolarVengeanceEngine;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SCG.SolarVengeance
{
    public static class ScenarioPreviewGenerator
    {
        //Generate a Glyph representing the Scenario
        public static Bitmap GeneratePreview(Scenario scen, int width, int height)
        {
            SVGame dummy = new SVGame(null);

            try
            {
                scen.BuildScenaio(dummy, null, 2, 20, 100, null);
            }
            catch(Exception ex)
            {
                MessageBox.Show("Error generating Scenario preview for " + scen.Name + ": " + ex.Message);
            }

            Bitmap bmp = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
            Graphics g = Graphics.FromImage(bmp);
            using (g)
            {
                g.Clear(Color.Black);
                double xmult = (double)width / (double)dummy.Width;
                double ymult = (double)height / (double)dummy.Height;

                //nebula
                int dx, dy;
                for (int x = 0; x < dummy.Width; x++)
                    for (int y = 0; y < dummy.Height; y++)
                        if (dummy.GetTerrain(x, y) == 'N')
                        {
                            dx = (int)(x * xmult);
                            dy = (int)(y * ymult);
                            g.DrawRectangle(Pens.Blue, dx, dy, 1, 1);
                        }

                //starsystems
                Brush sysBrush = new SolidBrush(Color.FromArgb(120, Color.Silver));
                foreach (StarSystem system in dummy.StarSystems)
                {
                    dx = (int)(system.X * xmult);
                    dy = (int)(system.Y * ymult);
                    g.FillEllipse(sysBrush, dx - 2, dy - 2, 5, 5);
                }

                //wormholes
                Pen whPen = new Pen(Color.FromArgb(120, Color.Fuchsia));
                Pen destPen = new Pen(Color.FromArgb(120, Color.Olive));
                foreach (Wormhole wh in dummy.Wormholes)
                {
                    dx = (int)(wh.X * xmult);
                    dy = (int)(wh.Y * ymult);
                    g.DrawEllipse(whPen, dx - 1, dy - 1, 3, 3);
                    int ddx = (int)(wh.Destination.X * xmult);
                    int ddy = (int)(wh.Destination.Y * ymult);
                    g.DrawLine(destPen, dx, dy, ddx, ddy);
                }

                //pulsars
                Pen pulsarPen = new Pen(Color.FromArgb(120, Color.Cyan));
                foreach (Pulsar p in dummy.Pulsars)
                {
                    dx = (int)(p.X * xmult);
                    dy = (int)(p.Y * ymult);
                    g.DrawEllipse(pulsarPen, dx - 1, dy - 1, 3, 3);
                }

                //black holes
                Pen bhPen = new Pen(Color.FromArgb(120, Color.Yellow));
                bhPen.Width = 2;
                foreach (BlackHole bh in dummy.BlackHoles)
                {
                    dx = (int)(bh.X * xmult);
                    dy = (int)(bh.Y * ymult);
                    g.DrawEllipse(bhPen, dx - 2, dy - 2, 4, 4);
                }
            }
            return bmp;
        }
    }
}
